<?php
/*----------------------------------------------------------------
 * 版权所有 2019~2020 极盾工作室  kplphp地址[ http://www.kplphp.com ]
 * 作者由JefferyCai码云所创造 [ https://gitee.com/JefferyCai ]
 * 当前码云地址 与 操作文档都在 [ https://gitee.com/JefferyCai/kplphp ]
 * QQ群请加 972703635 [ https://jq.qq.com/?_wv=1027&k=5YnmIH8 ]，如有更多服务，请单独加群主: 1345199080
 * 测试功能
----------------------------------------------------------------*/
namespace app\admin\controller;
use app\common\controller\Kbuilder;
use app\AdminController;
use app\common\model\Csvceshi;
use think\App;
use util\PHPCsv;

class Ceshi extends AdminController
{
    # 首页[默认]   前后端分离
    public function index()
    {
        $w = input('param');
        $list = Csvceshi::where($w)->paginate();
        foreach ($list as $k => $v)
        {
            $list[$k]['ceshi'] = '这是测试数据';
        }
        $searchField = ['name|hello'=>'姓名','telphone'=>'手机号'];
        # 查看所需要的字段
        $seeField = [
            ['id', 'ID','注释信息','col-sm-3'],
            ['name', '姓名','注释信息','col-sm-3'],
            ['telphone', '学校电话','注释信息','col-sm-3'],
            ['username', '学生姓名','注释信息','col-sm-3'],
            ['phone', '手机号码','注释信息','col-sm-3'],
        ];
        # 操作[编辑/添加]所需要的字段
        $editAddField = [
            ['text','name', '姓名','placeholder','表单类型为: text',''],
            ['text','telphone', '学校电话','placeholder','表单类型为: text','disabled'],
            ['password','password', '校园网密码','placeholder','表单类型为: password',''],
            ['switch','status', '状态','placeholder','表单类型为: radio',''],
            ['select','hobby', '是否喜欢女性还是男性','placeholder','表单类型为: select','',[0=>'喜欢男的',1=>'喜欢女的',2=>'两个都喜欢']],
            ['select2','hobby', '是否喜欢女性还是男性','placeholder','表单类型为: select2','',[0=>'喜欢男的',1=>'喜欢女的',2=>'两个都喜欢']],
            ['multiple_use','hobby', '是否喜欢女性还是男性','placeholder','表单类型为: multiple_use','',[0=>'喜欢男的',1=>'喜欢女的',2=>'两个都喜欢'],'custom-control-inline'],
            ['checkboxs','hobby', '喜欢哪些类型的爱好','placeholder','表单类型为: checkboxs','',[0=>'篮球',1=>'羽毛球',2=>'足球',3=>'橄榄球'],'custom-control-inline'],
            ['radio','hobby', '最喜欢哪一个类型的爱好','placeholder','表单类型为: radio','',[0=>'篮球',1=>'羽毛球',2=>'足球',3=>'橄榄球'],'custom-control-inline'],
            ['tags','hobby', '请定义自己的标签','请输入','表单类型为: tags','',[0=>'KPLPHP',1=>'kplphp',2=>'HTML5',3=>'CSS3']],
            ['time','start_time', '选择自己的时间标签','请输入','表单类型为: time',''],
            ['datetime','start_time', '选择自己的时间标签','请输入','表单类型为: datetime',''],
        ];
        $columns = [
            ['id', 'ID'],
            ['name', '学校名称'],
            ['telphone', '学校电话'],
            ['username', '学生姓名'],
            ['phone', '手机号码'],
            ['rightbtn', '操作'],
        ];
        return Kbuilder::sets('table')
            ->setSearchField($searchField)
            ->setEditAddField($editAddField)
            ->setSeeField($seeField)
            ->setRightBtn()
            ->setDataColumns($columns) # 设置字段数据列表
            ->setDataList($list) # 设置渲染数据
            ->view();
    }

    # 表单构造器
    public function table()
    {
        $w = [];
        /*
        $keyname = trim(input('keyname',''));
        if(!empty($keyname))
        {
            $w[] = ['name,telphone','like','%'.$keyname.'%'];
        }
        */
//        Kbuilder::kplbuilder('table')->twofunc();

        $list = Csvceshi::where($w)->paginate();
        if(request()->isPost())
        {
            echo json_encode(['data'=>$list]);exit;
        }
        return view('table');
    }

    public function form()
    {
        $list = array();
        return view('form',['list'=>$list]);
    }







    # 导入CSV格式测试
    public function import_csv()
    {
        if(request()->isPost())
        {
            # 定义字段
            $data['name'] = '';
            $data['telphone'] = '';
            $data['username'] = '';
            $data['phone'] = '';
            $insertData = PHPCsv::importCsv('file',$data,0);
            # 添加到数据库
            foreach ($insertData as $k => $v)
            {
                Csvceshi::create([
                    'name' => $v['name'],
                    'telphone' => $v['telphone'],
                    'username' => $v['username'],
                    'phone' => $v['phone'],
                ]);
            }
//            halt($insertData);
            $this->success('操作成功');
        }
        $list = Csvceshi::select()->toArray();
        return view('ceshi/import_csv',['list'=>$list]);
    }






}